package com.decent.ejfadmin.finance.controller;

import cn.decent.component.shiro.entity.Admin;
import cn.decent.component.shiro.service.AdminService;
import com.decent.ejfadmin.common.bean.MessageBean;
import com.decent.ejfadmin.common.bean.Page;
import com.decent.ejfadmin.common.enums.UserAddMoneyTypeEnum;
import com.decent.ejfadmin.finance.request.SupplyCardBalanceParam;
import com.decent.ejfadmin.finance.request.SupplyCardDetailParam;
import com.decent.ejfadmin.finance.service.SupplyCardService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

/**
 * 销卡
 *
 * @author zhangchao
 * @date 2018/7/28 8:44
 */
@Slf4j
@RestController
@RequestMapping("supplyCard")
public class SupplyCardController {

    @Autowired
    private SupplyCardService supplyCardService;
    @Autowired
    private AdminService adminService;

    /**
     * 查询销卡明细分页数据
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "findSupplyCardDetailPage", method = RequestMethod.GET)
    public Page findSupplyCardDetailPage(SupplyCardDetailParam param) {
        Page page = new Page();
        try {
            page = supplyCardService.findSupplyCardDetailPage(param);
        } catch (Exception e) {
            page.setTotal(0);
            log.warn("SupplyCardController findSupplyCardDetailPage occurs error Internal Server Error  ", e);
        }
        return page;
    }

    /**
     * 查询销卡余额分页数据
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "findSupplyCardBalancePage", method = RequestMethod.GET)
    public Page findSupplyCardBalancePage(SupplyCardBalanceParam param) {
        Page page = new Page();
        try {
            page = supplyCardService.findSupplyCardBalancePage(param);
        } catch (Exception e) {
            page.setTotal(0);
            log.warn("SupplyCardController findSupplyCardBalancePage occurs error Internal Server Error  ", e);
        }
        return page;
    }

    /**
     * 会员销卡明细导出
     *
     * @param param
     * @param response
     * @return
     */
    @RequestMapping(value = "supplyCardDetailDownLoad")
    public void supplyCardDetailDownLoad(SupplyCardDetailParam param, HttpServletResponse response) {
        ServletOutputStream writer = null;
        try {
            response.setCharacterEncoding("GBK");
            response.setContentType("text/html;charset=GBK");
            writer = response.getOutputStream();
            if (org.apache.commons.lang.StringUtils.isBlank(param.getStartTime()) || org.apache.commons.lang.StringUtils.isBlank(param.getEndTime())) {
                String msg = "<script >alert('输入起始时间！');</script>";
                IOUtils.write(msg.getBytes("GBK"), writer);
                writer.flush();
                return;
            }
            supplyCardService.supplyCardDetailDownLoad(param, response);
        } catch (Exception e) {
            log.warn("会员销卡明细导出出错[{}]", e);
        } finally {
            IOUtils.closeQuietly(writer);
        }
    }
    /**
     * 会员销卡余额导出
     *
     * @param param
     * @param response
     * @return
     */
    @RequestMapping(value = "supplyCardBalanceDownLoad")
    public void supplyCardBalanceDownLoad(SupplyCardBalanceParam param, HttpServletResponse response) {
        ServletOutputStream writer = null;
        try {
            response.setCharacterEncoding("GBK");
            log.info("supplyCardBalanceDownLoad={}", param);
            response.setContentType("text/html;charset=GBK");
            writer = response.getOutputStream();
            supplyCardService.supplyCardBalanceDownLoad(param, response);
        } catch (Exception e) {
            log.warn("会员销卡余额导出出错[{}]", e);
        } finally {
            IOUtils.closeQuietly(writer);
        }
    }

    /**
     * 收卡批量返佣
     *
     * @param data    源数据
     * @param addType 加款类型
     * @return MessageBean
     */
    @RequestMapping("newBatchSubmitUserAddMoneyApply")
    public MessageBean newBatchSubmitUserAddMoneyApply(String data, UserAddMoneyTypeEnum addType) {
        try {
            Admin currentUser = adminService.getCurrentAdmin();
            if (currentUser == null) {
                return MessageBean.fail("请先登录后再操作");
            }
            if (StringUtils.isBlank(data)) {
                return MessageBean.fail("请输入加款信息");
            }
            if (addType == null) {
                return MessageBean.fail("请选择加款类型");
            }
            return supplyCardService.newBatchSubmitUserAddMoneyApply(data, addType);
        } catch (Exception e) {
            log.warn("收卡批量返佣过程出错[{}]", e);
            return MessageBean.fail(StringUtils.defaultIfBlank(e.getLocalizedMessage(), "收卡批量返佣过程出错 "));
        }
    }


}
